/**
 * 
 */
package org.mspring.mlog.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.mspring.mlog.dao.PostDao;
import org.mspring.mlog.entity.Catalog;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.SysUser;
import org.mspring.mlog.entity.Tag;
import org.mspring.mlog.service.PostService;
import org.mspring.platform.core.AbstractServiceSupport;
import org.mspring.platform.persistence.query.QueryCriterion;
import org.mspring.platform.persistence.support.Page;
import org.mspring.platform.utils.Constants;
import org.mspring.platform.utils.DateUtils;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author fairchildy
 * @since 2012-7-23
 * @Description
 * @TODO
 */
@Service
@Transactional
public class PostServiceImpl extends AbstractServiceSupport implements PostService {

	@Autowired
	private PostDao postDao;
    
    /* (non-Javadoc)
     * @see org.mspring.mlog.service.PostService#trash2Publish(java.lang.Long[])

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#deletePost(java.lang.Long[])
     */


    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#findPost(org.mspring.platform.
     * persistence.support.Page,
     * org.mspring.platform.persistence.query.QueryCriterion)
     */
    @Override
    public Page<Post> findPost(Page<Post> page, QueryCriterion queryCriterion) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#findPost(org.mspring.platform.
     * persistence.support.Page, java.lang.String)
     */
    @Override
    public Page<Post> findPost(Page<Post> page, String queryString) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#findPost(org.mspring.platform.
     * persistence.support.Page, java.lang.String, java.lang.Object[])
     */
    @Override
    public Page<Post> findPost(Page<Post> page, String queryString, Object... queryParams) {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#getRecentPost(int)
     */
    @Override
    public List<Post> getRecentPost(final int nums) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#getMostViewPost(int)
     */
    @Override
    public List<Post> getMostViewPost(final int nums) {
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.PostService#getPostByTitle(java.lang.String)
     */
    @Override
    public Post getPostByTitle(String title) {
        // TODO Auto-generated method stub
    	return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#titleExists(java.lang.String ,
     * java.lang.Long)
     */
    @Override
    public boolean titleExists(String title, Long postId) {
        // TODO Auto-generated method stub
    	return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#urlExists(java.lang.String)
     */
    @Override
    public boolean urlExists(String url) {
        // TODO Auto-generated method stub
        return urlExists(url, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#urlExists(java.lang.String,
     * java.lang.Long)
     */
    @Override
    public boolean urlExists(String url, Long postId) {
        // TODO Auto-generated method stub
    	return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.PostService#updatePostCommentCount(java.lang
     * .Long)
     */
    @Override
    public void updatePostCommentCount(Long postId) {
        // TODO Auto-generated method stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.PostService#updatePostViewCount(java.lang.Long)
     */
    @Override
    public void updatePostViewCount(Long postId) {
        // TODO Auto-generated method stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#getRandomPost(int)
     */
    @Override
    public List<Post> getRandomPost(int nums) {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.mlog.service.PostService#getRelatedPost(org.mspring.mlog.
     * entity.Post, int)
     */
    @Override
    public List<Post> getRelatedPost(Post post, int nums) {
        // TODO Auto-generated method stub
        Set<Tag> tags = post.getTags();

        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.PostService#findAll()
     */
    @Override
    public List<Post> findAll() {
        // TODO Auto-generated method stub
    	return null;
    }

    /* (non-Javadoc)
     * @see org.mspring.mlog.service.PostService#getPostUrlByCommentId(java.lang.Long)
     */
    @Override
    public String getPostUrlByCommentId(Long commentId) {
        // TODO Auto-generated method stub
    	return null;
    }

    /* (non-Javadoc)
     * @see org.mspring.mlog.service.PostService#getPostByComment(java.lang.Long)
     */
    @Override
    public Post getPostByComment(Long commentId) {
        // TODO Auto-generated method stub
    	return null;
    }

    /* (non-Javadoc)
     * @see org.mspring.mlog.service.PostService#clearTrash()
     */
    @Override
    public void clearTrash() {
        // TODO Auto-generated method stub
    	return ;
    }

	@Override
	public Page<Post> findPost(Page<Post> postPage, SysUser user, Post postPmt) {
		int pageFrom = (postPage.getPageNo() - 1)* postPage.getPageSize();
		int pageCount = 0;
		Post post = new Post();
		post.setPageFrom(pageFrom);
		post.setStatus(postPage.getStatus());
		post.setuSER_ID(postPage.getuSER_ID());
		String title = StringUtils.trimHanSpace(postPmt.getTitle());
		// 防止SQL注入
		if (!StringUtils.isNullString(title)) {
			title = StringUtils.transfer(title);
		}
		post.setTitle(title);
		if (postPage.getTotalCount() < 0) {
			pageCount = postDao.getPageCount(post);
			postPage.setTotalCount(pageCount);
			postPage.setTotalPages(pageCount/postPage.getPageSize() + 1);
		}
		List<Post> list = postDao.getPage(post);
		List<Post> modifyList = new ArrayList<Post>();
		if (null != list && list.size() > 0) {
			for(Post p :list) {
				List<Catalog> catalogList = postDao.getPostCatalog(p);
				Set<Catalog> catalog = new HashSet<Catalog>();
				for(Catalog c :catalogList) {
					catalog.add(c);
				}
				
				List<Tag> tagList = postDao.getPostTag(p);
				Set<Tag> tag = new HashSet<Tag>();
				for(Tag t :tagList) {
					tag.add(t);
				}
				p.setTags(tag);
				p.setCatalogs(catalog);
				p.setAuthor(user);
				modifyList.add(p);
			}
		}
		postPage.setResult(modifyList);
		return postPage;
	}

	@Override
	public Post getPostById(Post post) {

		Post p = postDao.getPostById(post);
		
		List<Catalog> catalogList = postDao.getPostCatalog(p);
		Set<Catalog> catalog = new HashSet<Catalog>();
		for(Catalog c :catalogList) {
			catalog.add(c);
		}
		List<Tag> tagList = postDao.getPostTag(p);
		Set<Tag> tag = new HashSet<Tag>();
		for(Tag t :tagList) {
			tag.add(t);
		}
		p.setTags(tag);
		p.setCatalogs(catalog);
		
		return p;
	}
    @Override
    public void updatePost(Post post) {
    	
    	// TODO post
    	post.setModifyTime(DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS));
    	
    	postDao.updatePost(post);
    	// TODO catalog
    	HashSet<Catalog> catalog =  (HashSet<Catalog>) post.getCatalogs();
    	for(Catalog c : catalog) {
    		Post param = new Post();
    		param.setId(post.getId());
    		param.setuSER_ID(post.getuSER_ID());
    		param.setCatalogId(c.getId());
    		postDao.updateCatalogs(param);
    	}
    	
    	// TODO tag
    	HashSet<Tag> tag =  (HashSet<Tag>) post.getTags();
    	if(null != tag && tag.isEmpty()) {
        	for(Tag c : tag) {
        		Post param = new Post();
        		param.setId(post.getId());
        		param.setuSER_ID(post.getuSER_ID());
        		param.setTagName(c.getName());
        		param.setCreateTime(DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS));
        		List<Post> result = postDao.selectTag(param);
        		Post rltPost = new Post();
        		if (null != result && result.size() > 0) {
        			rltPost = result.get(0);
        		} else {
        			postDao.insertTag(param);
            		List<Post> rlt = postDao.selectTag(param);
            		if (null != rlt && rlt.size() > 0) {
            			rltPost = rlt.get(0);
            		}
        		}
        		param.setTagId(rltPost.getId());
        		postDao.updateTags(param);
        	}
    	}

    	
    }

	@Override
	public boolean urlExists(Post post) {
		List<Post>  result = postDao.urlExists(post);
		if (null != result&&result.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public Post createPost(Post post) {
    	post.setCreateTime(DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS));
    	postDao.insertPost(post);
    	List<Post> result = postDao.getPostByTime(post);
    	if (null != result && result.size() > 0) {
    		post.setId(result.get(0).getId());
        	HashSet<Catalog> catalog =  (HashSet<Catalog>) post.getCatalogs();
        	for(Catalog c : catalog) {
        		Post param = new Post();
        		param.setId(post.getId());
        		param.setuSER_ID(post.getuSER_ID());
        		param.setCatalogId(c.getId());
        		postDao.updateCatalogs(param);
        	}
    	}

    	
		return null;
	}
    @Override
    public void deletePost(Long... idArray) {
        // TODO Auto-generated method stub
    }

	@Override
	public void deletePost(Post post) {
		postDao.deleteCatalogById(post);
		postDao.deletePostById(post);
	}

	@Override
	public void discardPost(Post post) {
		post.setCreateTime(DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS));
		post.setStatus(Constants.POST_STATUS_TRASH);
		postDao.updatePostStatus(post);
		
	}

	@Override
	public void trash2Publish(Post post) {
		post.setCreateTime(DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS));
		post.setStatus(Constants.POST_STATUS_PUBLISH);
		postDao.updatePostStatus(post);
	}

	@Override
	public Post getPostByUrl(String url) {
		
		// 不是文章的情况，直接返回
		if (StringUtils.isNullString(url) || !url.startsWith("/post")) {
			Post post = new Post();
			Set<Catalog> catalog = new HashSet<Catalog>();
			post.setCatalogs(catalog);
			return post;
		}
		// SQL注入防止
		url = StringUtils.transfer(url);
		Post post = postDao.getPostByUrl(url);
		
		if(null == post) {
			post = new Post();
			post.setTitle("");
			post.setContent("");
		}
		List<Catalog> catalogList = postDao.getPostCatalog(post);
		Set<Catalog> catalog = new HashSet<Catalog>();
		for(Catalog c :catalogList) {
			catalog.add(c);
		}
//		List<Tag> tagList = postDao.getPostTag(post);
//		Set<Tag> tag = new HashSet<Tag>();
//		for(Tag t :tagList) {
//			tag.add(t);
//		}
//		post.setTags(tag);
		post.setCatalogs(catalog);
		
		return post;
	}
}
